package top.abilities.util.util;


import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.abilities.util.util.httpClient.SimpleHttpUtils;
import top.abilities.util.util.validate.RegularUtil;
import top.abilities.util.utils.MessageUtils;

import java.io.IOException;
import java.util.*;

/**
 * 手机短信操作类
 *
 * @author dong
 * @create 2018-02-01 14:29
 **/
public class MobileMessageUtil {

    private static Logger _log = LoggerFactory.getLogger(MobileMessageUtil.class);

    public static boolean sendMessage(String content,String phone){
        String appSecret="6c8g5b7xb3df20cv84dfv21v52cm6iu9";
        String appAccount="wallet";
        Map paramMap=new LinkedHashMap();
        paramMap.put("PhoneNum",phone);
        paramMap.put("PhoneContent",content);
        paramMap.put("ElectType","GAP");
        paramMap.put("ElectSource","GAP");
        paramMap.put("IsNowSend","1");
        paramMap.put("IsNeedTry","1");

        // 国内短信
        if (RegularUtil.validatePattern("^[1]([3-9])[0-9]{9}$",phone)){
            String urlInner="https://api.fesco.com.cn/Api/SendMessage/SendMessage";
            return sendMessageIn(paramMap, urlInner, appSecret, appAccount);
        }else if(RegularUtil.validatePattern("^(852|853|886)\\d*$",phone)){ // 国际短信
            String urlInner="https://api.fesco.com.cn/Api/SendMessage/SendMessageInt";
            paramMap.put("IsInternational","true");
            return sendMessageIn(paramMap, urlInner, appSecret, appAccount);
        }else {
            return false;
        }
    }

    private static boolean sendMessageIn(Map paramMap, String urlInner, String appSecret, String appAccount) {
        long timespan = System.currentTimeMillis()/1000;
        String uuid= UUID.randomUUID().toString();

        Map extendMap=new LinkedHashMap();
        extendMap.put("appaccount", appAccount);
        extendMap.put("timestamp", String.valueOf(timespan));
        extendMap.put("noncestr", uuid);
        String signStr= MessageUtils.signString(paramMap,extendMap);
        //参数值拼接的字符串收尾添加appSecret值
        String waitSignStr = appSecret + signStr;
        //获取MD5加密后的字符串
        String sign = ParseMD5.parseStrToMd5U32(waitSignStr).toLowerCase();

        Map lastMap=new LinkedHashMap();
        lastMap.put("appaccount", appAccount);
        lastMap.put("timestamp", String.valueOf(timespan));
        lastMap.put("noncestr", uuid);
        lastMap.put("jsonParam",paramMap);
        lastMap.put("sign", sign);
        try {
            String param= Jsons.objToJson(lastMap);
            _log.info("短信发送入参："+param);
            String Re= SimpleHttpUtils.doPostJson(urlInner,param);
            _log.info("短信发送出参："+Re);
        } catch (IOException e) {
            e.printStackTrace();
            _log.info("短信发送错误："+e.getMessage());
        }
        return true;
    }


    public static void main(String[] args) throws IOException {
        sendMessage("您的验证码是[243234],五分钟之内有效","15321878373");
    }
}
